/**
 * 回收站模块类型定义
 * 基于后端API文档：我的模块API.md
 */

// ==================== 基础类型 ====================

/**
 * 回收站项目类型
 */
export type RecycleBinItemType = 'book_set' | 'book' | 'audio' | 'reading_record' | 'favorite'

/**
 * 回收站项目
 */
export interface RecycleBinItem {
  id: number
  itemType: RecycleBinItemType
  itemId: number
  title: string
  cover?: string
  bookCount?: number // 仅绘本集
  deletedAt: string // ISO格式时间字符串
  expireAt?: string // ISO格式时间字符串
  // 以下字段用于前端显示，可能不在后端响应中
  groupTitle?: string
}

/**
 * 回收站分组
 */
export interface RecycleBinGroup {
  groupTitle: string
  items: RecycleBinItem[]
}

/**
 * 回收站页面响应
 */
export interface RecycleBinPageRespVO {
  total: number
  groups: RecycleBinGroup[]
}

// ==================== 请求类型 ====================

/**
 * 分页请求参数
 */
export interface PageParams {
  page: number
  size: number
}

/**
 * 批量还原请求
 */
export interface RecycleBinBatchRestoreReqVO {
  itemIds: number[]
}

// ==================== API响应包装类型 ====================

/**
 * 通用API响应格式
 */
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

/**
 * 获取回收站内容API响应
 */
export type GetRecycleBinResponse = ApiResponse<RecycleBinPageRespVO>

/**
 * 还原项目API响应
 */
export type RestoreItemResponse = ApiResponse<void>

/**
 * 删除项目API响应
 */
export type DeleteItemResponse = ApiResponse<void>

/**
 * 批量还原API响应
 */
export type BatchRestoreResponse = ApiResponse<void>

/**
 * 清空回收站API响应
 */
export type ClearRecycleBinResponse = ApiResponse<void>

// ==================== 错误码类型 ====================

/**
 * 回收站模块错误码
 */
export enum RecycleBinErrorCode {
  ITEM_NOT_FOUND = 50007,
  RESTORE_FAILED = 50008,
}

/**
 * 错误信息映射
 */
export const RecycleBinErrorMessages: Record<RecycleBinErrorCode, string> = {
  [RecycleBinErrorCode.ITEM_NOT_FOUND]: '回收站项目不存在',
  [RecycleBinErrorCode.RESTORE_FAILED]: '还原失败',
}

// ==================== 组件Props类型 ====================

/**
 * 回收站列表组件Props
 */
export interface RecycleBinListProps {
  groups: RecycleBinGroup[]
  loading?: boolean
  hasMore?: boolean
  onLoadMore?: () => void
  onRestore?: (item: RecycleBinItem) => void
  onDelete?: (item: RecycleBinItem) => void
  onBatchRestore?: (items: RecycleBinItem[]) => void
  onClearAll?: () => void
}

/**
 * 回收站分组组件Props
 */
export interface RecycleBinGroupProps {
  group: RecycleBinGroup
  onRestore?: (item: RecycleBinItem) => void
  onDelete?: (item: RecycleBinItem) => void
  onSelectItem?: (item: RecycleBinItem, selected: boolean) => void
  selectedItems?: Set<number>
}

/**
 * 回收站项目组件Props
 */
export interface RecycleBinItemProps {
  item: RecycleBinItem
  onRestore?: (item: RecycleBinItem) => void
  onDelete?: (item: RecycleBinItem) => void
  onSelect?: (item: RecycleBinItem, selected: boolean) => void
  selected?: boolean
}

/**
 * 回收站操作栏Props
 */
export interface RecycleBinActionsProps {
  selectedItems: RecycleBinItem[]
  onBatchRestore?: (items: RecycleBinItem[]) => void
  onBatchDelete?: (items: RecycleBinItem[]) => void
  onClearAll?: () => void
  onSelectAll?: () => void
  onClearSelection?: () => void
  totalCount?: number
}

// ==================== 状态管理类型 ====================

/**
 * 回收站模块状态
 */
export interface RecycleBinState {
  groups: RecycleBinGroup[]
  selectedItems: Set<number>
  loading: boolean
  hasMore: boolean
  total: number
  error: string | null
}

/**
 * 回收站模块Actions
 */
export interface RecycleBinActions {
  getRecycleBinContent: (params: PageParams) => Promise<void>
  restoreItem: (itemId: number) => Promise<void>
  deleteItem: (itemId: number) => Promise<void>
  batchRestore: (itemIds: number[]) => Promise<void>
  clearRecycleBin: () => Promise<void>
  selectItem: (itemId: number, selected: boolean) => void
  selectAll: () => void
  clearSelection: () => void
  clearError: () => void
  resetData: () => void
}

// ==================== 工具类型 ====================

/**
 * 回收站项目类型配置
 */
export interface RecycleBinTypeConfig {
  key: RecycleBinItemType
  label: string
  icon: string
  color: string
}

/**
 * 默认项目类型配置
 */
export const DEFAULT_RECYCLE_BIN_TYPES: RecycleBinTypeConfig[] = [
  {
    key: 'book_set',
    label: '绘本集',
    icon: 'folder',
    color: '#4ecdc4',
  },
  {
    key: 'book',
    label: '绘本',
    icon: 'book',
    color: '#ff6b6b',
  },
  {
    key: 'audio',
    label: '音频',
    icon: 'sound',
    color: '#45b7d1',
  },
  {
    key: 'reading_record',
    label: '阅读记录',
    icon: 'time',
    color: '#ffa726',
  },
  {
    key: 'favorite',
    label: '收藏',
    icon: 'heart',
    color: '#e91e63',
  },
]

/**
 * 过期状态
 */
export type ExpireStatus = 'normal' | 'warning' | 'expired'

/**
 * 过期状态配置
 */
export interface ExpireStatusConfig {
  status: ExpireStatus
  label: string
  color: string
  threshold: number // 剩余天数阈值
}

/**
 * 默认过期状态配置
 */
export const DEFAULT_EXPIRE_STATUS: ExpireStatusConfig[] = [
  {
    status: 'normal',
    label: '正常',
    color: '#4caf50',
    threshold: 7,
  },
  {
    status: 'warning',
    label: '即将过期',
    color: '#ff9800',
    threshold: 3,
  },
  {
    status: 'expired',
    label: '已过期',
    color: '#f44336',
    threshold: 0,
  },
]

/**
 * 分页默认配置
 */
export const DEFAULT_PAGE_CONFIG = {
  page: 1,
  size: 20,
}

/**
 * 回收站保留天数
 */
export const RECYCLE_BIN_RETENTION_DAYS = 30

/**
 * 批量操作配置
 */
export interface BatchOperationConfig {
  maxItems: number // 最大批量操作数量
  confirmThreshold: number // 需要确认的数量阈值
}

/**
 * 默认批量操作配置
 */
export const DEFAULT_BATCH_CONFIG: BatchOperationConfig = {
  maxItems: 50,
  confirmThreshold: 10,
}

/**
 * 排序选项
 */
export type RecycleBinSortType = 'delete_time_desc' | 'delete_time_asc' | 'expire_time_asc' | 'type'

/**
 * 排序配置
 */
export interface SortConfig {
  key: RecycleBinSortType
  label: string
  icon: string
}

/**
 * 默认排序配置
 */
export const DEFAULT_SORT_OPTIONS: SortConfig[] = [
  {
    key: 'delete_time_desc',
    label: '最近删除',
    icon: 'time',
  },
  {
    key: 'delete_time_asc',
    label: '最早删除',
    icon: 'time-outline',
  },
  {
    key: 'expire_time_asc',
    label: '即将过期',
    icon: 'warning',
  },
  {
    key: 'type',
    label: '按类型',
    icon: 'list',
  },
]

/**
 * 时间格式化选项
 */
export interface TimeFormatOptions {
  showTime?: boolean
  relative?: boolean
  format?: string
}

/**
 * 默认时间格式化选项
 */
export const DEFAULT_TIME_FORMAT: TimeFormatOptions = {
  showTime: true,
  relative: true,
  format: 'YYYY-MM-DD HH:mm',
}
